import random
from datetime import datetime

from reportlab.graphics.barcode import code128
from reportlab.lib.units import mm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas

from generator.address_generator import AddressGenerator
from generator.name_generator import NameGenerator
from generator.phone_generator import PhoneGenerator
from generator.product_name_generator import ProductNameGenerator


def get_image_position(pdf_canvas, barcode):
    global page_width_mm
    barcode_width = barcode.width
    page_width, page_height = pdf_canvas._pagesize
    x_position = (page_width - barcode_width) / 2
    points_to_mm = 25.4 / 72
    page_width_mm = x_position * points_to_mm
    return page_width_mm


def get_middle_position(pdf_canvas, barcode_data):
    global page_width_mm
    text_width = pdfmetrics.stringWidth(barcode_data, "msyh", 16 * mm)
    page_width, page_height = pdf_canvas._pagesize
    x_position = (page_width - text_width) / 2
    points_to_mm = 25.4 / 72
    page_width_mm = x_position * points_to_mm
    return page_width_mm


class LabelGenerator:

    @staticmethod
    def generate_label_image(brand, pdf_path, barcode_data):
        pdf_canvas = canvas.Canvas(pdf_path, pagesize=(480 * mm, 680 * mm))

        # 定义字体大小
        font_size = 14 * mm
        pdfmetrics.registerFont(TTFont("msyh", "static/fonts/msyh.ttc"))

        # 添加文本
        pdf_canvas.setFont("msyh", font_size)
        pdf_canvas.drawString(15 * mm, 660 * mm, brand.desc)
        pdf_canvas.drawString(15 * mm, 640 * mm, "EXPRESS")

        pdf_canvas.setFontSize(30 * mm)
        pdf_canvas.drawString(320 * mm, 650 * mm, "快递包裹")

        pdf_canvas.setLineWidth(2 * mm)
        pdf_canvas.line(10 * mm, 625 * mm, 470 * mm, 625 * mm)
        pdf_canvas.setLineWidth(1 * mm)

        barcode = code128.Code128(barcode_data, barHeight=45 * mm, barWidth=1.5 * mm)
        barcode.drawOn(pdf_canvas, get_image_position(pdf_canvas, barcode) * mm, 570 * mm)

        # 添加条形码下方的文字
        pdf_canvas.setFontSize(16 * mm)

        get_middle_position(pdf_canvas, barcode_data)
        pdf_canvas.drawString(get_middle_position(pdf_canvas, barcode_data) * mm, 545 * mm, barcode_data)
        pdf_canvas.setFontSize(14 * mm)

        pdf_canvas.line(10 * mm, 525 * mm, 470 * mm, 525 * mm)

        pdf_canvas.setFontSize(30 * mm)

        # 添加收件人信息
        pdf_canvas.drawString(10 * mm, 460 * mm, "收")

        pdf_canvas.setFontSize(14 * mm)
        pdf_canvas.drawString(80 * mm, 500 * mm, NameGenerator.generate_random_chinese_name())
        pdf_canvas.drawString(180 * mm, 500 * mm, PhoneGenerator.generate_random_phone_number())
        address = AddressGenerator.generate_random_address()
        detail = address.get_province() + address.get_city() + address.get_district() + address.get_address()
        pdf_canvas.drawString(80 * mm, 480 * mm, detail)

        pdf_canvas.line(10 * mm, 430 * mm, 470 * mm, 430 * mm)

        # 添加寄件人信息
        pdf_canvas.setFontSize(30 * mm)
        pdf_canvas.drawString(10 * mm, 370 * mm, "寄")

        pdf_canvas.setFontSize(14 * mm)
        pdf_canvas.drawString(80 * mm, 410 * mm, NameGenerator.generate_random_chinese_name())
        pdf_canvas.drawString(180 * mm, 410 * mm, PhoneGenerator.generate_random_phone_number())
        address = AddressGenerator.generate_random_address()
        detail = address.get_province() + address.get_city() + address.get_district() + address.get_address()
        pdf_canvas.drawString(80 * mm, 390 * mm, detail)

        pdf_canvas.line(10 * mm, 335 * mm, 470 * mm, 335 * mm)

        # 添加托寄物信息
        pdf_canvas.setFontSize(18 * mm)
        pdf_canvas.drawString(15 * mm, 300 * mm, "托寄物：")
        pdf_canvas.setFontSize(14 * mm)
        random_number = random.randint(1, 5)
        for i in range(random_number):
            height = 275 - i * 25
            product_name = ProductNameGenerator.generate_random_product_name()
            pdf_canvas.drawString(15 * mm, height * mm, product_name + " * " + str(random.randint(1, 10)))

        pdf_canvas.line(10 * mm, 160 * mm, 470 * mm, 160 * mm)

        # 添加条形码
        barcode = code128.Code128(barcode_data, barHeight=45 * mm, barWidth=1.5 * mm)
        barcode.drawOn(pdf_canvas, get_image_position(pdf_canvas, barcode) * mm, 100 * mm)

        # 添加条形码下方的文字
        pdf_canvas.setFontSize(16 * mm)
        pdf_canvas.drawString(get_middle_position(pdf_canvas, barcode_data) * mm, 80 * mm, barcode_data)
        pdf_canvas.setFontSize(14 * mm)

        pdf_canvas.line(10 * mm, 70 * mm, 470 * mm, 70 * mm)

        # 获取当前时间
        now = datetime.now()
        formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")

        # 添加日期
        pdf_canvas.drawString(15 * mm, 50 * mm, formatted_time)

        # 保存并关闭 PDF
        pdf_canvas.save()
        print(f"PDF saved as {pdf_path}")
